//
//  WJFTIndicatorController.m
//  JerryIos
//
//  Created by 王军 on 2018/2/8.
//  Copyright © 2018年 王军. All rights reserved.
//

#import "WJFTIndicatorController.h"
#import "WJCarBrand.h"
#import "WJCar.h"
#import "FTIndicator.h"
#import "FTToastIndicator.h"
#import "FTProgressIndicator.h"
#import "FTNotificationIndicator.h"

@interface WJFTIndicatorController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSArray *carBrandArray;
@property (nonatomic, assign) BOOL isLight;

@end

@implementation WJFTIndicatorController

- (NSArray *)carBrandArray {
    if (_carBrandArray == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"ftindicator.plist" ofType:nil];
        NSArray *dicArray = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *groupArray = [NSMutableArray array];
        for (NSDictionary *dic in dicArray) {
            WJCarBrand *carBrand = [WJCarBrand carBrandWithDict:dic];
            [groupArray addObject:carBrand];
        }
        _carBrandArray = groupArray;
    }
    return _carBrandArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _isLight = YES;
    
    // 创建NavBar
    [self setupNavBar];
    // 创建tableView
    [self setupTableView];
}

// 创建NavBar
- (void)setupNavBar {
    //右边的按钮
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:[UIImage imageNamed:@"icon_share_normal"]
                                                                  highImage:[UIImage imageNamed:@"icon_share_selected"] target:self
                                                                     action:@selector(share)];
    //中间的标题
    self.navigationItem.title = @"FTIndicator";
    
}

-(void)share{
    _isLight = !_isLight;
    
    //FTIndicator
    [FTIndicator setIndicatorStyle:self.isLight ? UIBlurEffectStyleLight : UIBlurEffectStyleDark];
    [FTIndicator showInfoWithMessage:self.isLight ? @"白天模式打开" : @"黑夜模式打开"];
}

// 创建tableView
- (void)setupTableView {

    //TableView的Frame
    CGFloat topHeight = [[UIApplication sharedApplication] statusBarFrame].size.height + 44;
    self.tableView.frame = CGRectMake(0, topHeight, self.view.bounds.size.width, self.view.bounds.size.height - topHeight);
    //指定代理
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.carBrandArray.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    WJCarBrand *carBrand = self.carBrandArray[section];
    return carBrand.title;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    WJCarBrand *carBrand = self.carBrandArray[section];
    return carBrand.carArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:cellId];
    }
    WJCarBrand *carBrand = self.carBrandArray[indexPath.section];
    WJCar *car = carBrand.carArray[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:car.icon];
    cell.textLabel.text = car.name;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.section) {
        case 0:
            [self showNotificatio:indexPath.row];
            break;
        case 1:
            [self showProgressHUD:indexPath.row];
            break;
        case 2:
            [self showToast:indexPath.row];
            break;
        default:
            break;
    }
}

-(void) showNotificatio: (NSInteger)row{
    switch (row) {
        case 0:
            [FTIndicator showNotificationWithImage:[UIImage imageNamed:@"icon_home"]
                                             title:@"这是一个通知标题"
                                           message:@"这是一个通知消息，滑动删除或点击删除。"];
            break;
        case 1:
            [FTIndicator showNotificationWithTitle:@"这是一个通知标题"
                                           message:@"这是一个通知消息，滑动删除或点击删除。"];
            break;
        case 2:
            [FTIndicator showNotificationWithImage:[UIImage imageNamed:@"icon_home"]
                                             title:@"这是一个通知标题"
                                           message:@"这是一个通知消息，滑动删除或点击删除。"
                                        tapHandler:^{
                                            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"点击事件"
                                                                                                message:@"你点击了通知"
                                                                                               delegate:nil
                                                                                      cancelButtonTitle:@"确定"
                                                                                      otherButtonTitles:nil];
                                            [alertView show];
                                        } completion:^{
                                            [FTIndicator showInfoWithMessage:@"去除通知"];
                                        }];
            break;
        case 3:
            [FTIndicator showNotificationWithImage:[UIImage imageNamed:@"maps_icon"]
                                             title:@"这是一个通知标题"
                                           message:@"这是一个通知消息，滑动删除或点击删除。"
                                       autoDismiss:NO
                                        tapHandler:^{
                                            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"点击事件"
                                                                                                message:@"你点击了通知"
                                                                                               delegate:nil
                                                                                      cancelButtonTitle:@"确定"
                                                                                      otherButtonTitles:nil];
                                            [alertView show];
                                        } completion:^{
                                            [FTIndicator showInfoWithMessage:@"去除通知"];
                                        }];
            break;
        case 4:
            [FTIndicator dismissNotification];
            break;
        default:
            break;
    }
}

-(void) showProgressHUD:(NSInteger) row {
    switch (row) {
        case 0:
            [FTIndicator showProgressWithMessage:@"这是一个进度消息" userInteractionEnable:YES];
            break;
        case 1:
            [FTIndicator showSuccessWithMessage:@"这是一个成功消息"];
            break;
        case 2:
            [FTIndicator showErrorWithMessage:@"这是一个错误消息（不带图标）"];
            break;
        case 3:
            [FTIndicator showErrorWithMessage:@"这是一个错误消息（可自定义图标）"
                                        image:[UIImage imageNamed:@"icon_home"]
                        userInteractionEnable:YES];
            break;
        case 4:
            [FTIndicator showInfoWithMessage:@"这是一个消息" userInteractionEnable:NO];
            break;
        case 5:
            [FTIndicator dismissProgress];
            break;
        default:
            break;
    }
}

-(void) showToast:(NSInteger) row {
    switch (row) {
        case 0:
            [FTIndicator showToastMessage:@"Short Toast."];
            break;
        case 1:
            [FTIndicator dismissToast];
            break;
        default:
            break;
    }
}
@end
